<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\CreateException;
use App\Exceptions\DeleteException;
use App\Team;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * @property Team model
 */
class TeamController extends Controller
{
    public function __construct(Team $model)
    {
        $this->model = $model;
    }

    public function all()
    {
        return $this->page($this->model->get(['id','name']));
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $page  = $request->get('page',1);
        $size  = $request->get('size',10);
        return $this->page($this->model->limit($size)->offset(($page-1)*$size)->get(),$this->model->count());
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws CreateException
     */
    public function store(Request $request)
    {
        $id = $this->model->create($request->only('name'));
        if ($id) {
            return $this->success();
        }
        throw new CreateException('创建资源失败');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }
   

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy($id)
    {
        if ($this->model->where(['id'=>$id])->delete()){
            return $this->success();
        }
        throw new DeleteException();
    }
}
